package personal.wang.handler;

import com.google.protobuf.InvalidProtocolBufferException;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import personal.wang.abs.AbstractMessageHandler;
import personal.wang.domain.TestpointInfoProto;
import personal.wang.manager.WorkerManager;
import personal.wang.message.Header;
import personal.wang.message.Message;
import personal.wang.enm.MessageType;

public class ToWorkerCommadMessageHandler extends AbstractMessageHandler {
    private static Logger logger = LoggerFactory.getLogger(ToWorkerCommadMessageHandler.class);
    private WorkerManager workerManager = null ;
    public ToWorkerCommadMessageHandler(WorkerManager workerManager){
       this.workerManager = workerManager ;
    }
    @Override
    public void handler(Message message, ChannelHandlerContext ctx) {

        logger.info("receiver {}" , message.getHeader());
        TestpointInfoProto.TestPointInfo tt = null ;
        try {
             tt = TestpointInfoProto.TestPointInfo.parseFrom(message.getContent());
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        }
        logger.info("cnt: {} , BootstrapServer: {}" , tt.getTplCnt() , tt.getKafkaBootstrap());
        Header header = message.getHeader();
        header.setFlag(MessageType.TEMPLATE_MESSAGE.getFlag());
        workerManager.broadcastMessageToWorker(message);
        super.handler(message, ctx);
    }
}
